// blast.js functions...

function get_blast_form() {
	var url = '/blasts/new';
	var now = new Date;
	var pars = 'blast[blasted_type]=' + blast.type + '&blast[blasted_id]=' + blast.id + '&blast[partial]=' + blast.partial + '&blast[url]=' + blast.url + '&layout=false&b=' +  now.getMilliseconds();
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars,
			onSuccess: get_blast_form_cb
		});
}

function get_blast_form_cb(res) {
	// init form values...
	$('blast_form').innerHTML = res.responseText;
	$('blast_to').value = blast.recipients;
	$('blast_from').value = blast.from;
	$('blast_subject').value = blast.subject;
	$('blast_message').value = blast.message;		
	$('blast_preview_partial').innerHTML = $('blast_preview_partial_hidden').innerHTML;
	// generate preview
	blast_preview();
	blast_hook_events();
}

function blast_hook_events() {
	// hook events..
	Event.observe($('blast_to'), 'keyup', blast_preview, false);
	Event.observe($('blast_from'), 'keyup', blast_preview, false);
	Event.observe($('blast_from'), 'keyup', function(){update_text_counter('blast_from', 'blast_from_count',50)}, false);
	Event.observe($('blast_subject'), 'keyup', blast_preview, false);
	Event.observe($('blast_subject'), 'keyup', function(){update_text_counter('blast_subject', 'blast_subject_count',100)}, false);
	Event.observe($('blast_message'), 'keyup', blast_preview, false);
}

function blast_preview() {
	// to
	$('blast_preview_to').innerHTML = "<strong>to:</strong> " + $('blast_to').value.stripTags().stripScripts();
	// from
	$('blast_preview_from').innerHTML = "<strong>from:</strong> " + $('blast_from').value.stripTags().stripScripts();
	// subject
	$('blast_preview_subject').innerHTML = "<strong>" + $('blast_subject').value.stripTags().stripScripts(); + "</strong>";
	// message
	$('blast_preview_message').innerHTML = $('blast_message').value.stripTags().stripScripts();
}
	
function blast_create(form) {
	//return false;
	if (!blast_validate()) {return false;}
	blast_button(true, "Sending...")
	
	var url = '/blasts/create';
	var pars = Form.serialize(form) + '&layout=false';

	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars,
			onSuccess: blast_create_cb,
			on500: blast_create_500_cb
		});
	return false;
}

function blast_create_cb(res) {
	blast_success("Your message should arrive in the next few minutes. " + res.responseText);
	// remove form and preview
	Element.remove('blast_form');
	Element.remove('blast_preview')	;
}	

function blast_create_500_cb(res) {
	// init form values...
	blast_error("<h4>Your blast has errors:</h4>")
	$('blast_form').innerHTML = res.responseText;
	$('blast_preview_partial').innerHTML = $('blast_preview_partial_hidden').innerHTML;
	blast_preview();
	blast_button(false, "Send")
}	

function blast_validate() {
	// document.forms['_blast_']		

	var errors = [];
	var to = $('blast_to');
	if (to.value == "") {
		errors.push("Please provide at least one recipient.")
	}
	var from = $('blast_from');
	if (from.value == "") {
		errors.push("Please complete who this blast is from.")
	}

	if (errors.length > 0) {
		blast_error("<h4>Your blast has errors:</h4>" + errors.bulleted());
		return false		
	} else {
		return true
	}	

}

function blast_error(msg) {
	var flash = $('blast_flash')
	flash.className = "error";
	flash.innerHTML = msg;
}

function blast_success(msg) {
	var flash = $('blast_flash')
	flash.className = "success";
	flash.innerHTML = msg;
}

function blast_button(bool, msg) {
	var btn = $('blast_send')
	btn.disabled = bool;
	btn.value = msg;
}

function update_text_counter(txt_id, cnt_id, cnt) {
	var txt_el = $(txt_id);
	var cnt_el = $(cnt_id);
	var txt = txt_el.value;
	var txt_length = txt.length;

	var remaining = ((cnt - txt_length) > 0) ? (cnt - txt_length) : 0; 
	cnt_el.innerHTML = '<b>remaining</b>:' + remaining;
	if (remaining == 0) {
		txt_el.value = txt.substring(0,cnt);
	}
}

Array.prototype.bulleted = function() {
	return "<ul><li>" + this.join("</li><li>") + "</li></ul>";
}
